/**
 * 枚举工具类
 * @Author Lonely_man
 * @Date 2020/3/11 11:08
 * @Version 1.0
 */
layui.define(['jquery','tool'], function (exports) {

    var $ = layui.jquery;
    var tool = layui.tool;

    var obj = {

        /**
         * 根据枚举类创建选择框
         * @param tragetId 目标select标签Id
         * @param enumClassName 枚举类的类名
         * @param ignoreOptions 忽略的选择项的值
         * @param selectOption  默认选择的值
         * @param firstOption  第一个操作值
         */
        createSelectByEnum : function (tragetId, enumClassName, ignoreOptions, selectOption, firstOption) {
            var url = "/back/enum/getAllEnums";
            $.ajax({
                url: url
                , type: 'post'
                , data: {
                    enumClassName: enumClassName
                }
                , async: false
                , dataType: 'json'
                , success: function (res) {
                    if (res.code === return_200) {
                        var selectHtml = "";
                        if(!tool.isEmpty(firstOption)){
                            selectHtml += "<option value=''>" + firstOption + "</option>";
                        }
                        $("#" + tragetId).empty();
                        for (var i = 0; i < res.data.length; i++) {
                            var flag = false;
                            if (ignoreOptions && ignoreOptions.length != 0) {
                                for (var j = 0; j < ignoreOptions.length; j++) {
                                    if (res.data[i].code == ignoreOptions[j]) {
                                        flag = true;
                                        break;
                                    }
                                }
                            }
                            if (flag) {
                                continue;
                            }
                            if (selectOption && res.data[i].code == selectOption) {
                                selectHtml += "<option value=\"" + res.data[i].code + "\" selected>" + res.data[i].name + "</option>";
                            } else {
                                selectHtml += "<option value=\"" + res.data[i].code + "\">" + res.data[i].name + "</option>";
                            }
                        }
                        $("#" + tragetId).append(selectHtml);
                    } else {
                        console.log("初始化选择框失败");
                    }
                }
                , error: function () {
                    console.log("初始化选择框异常");
                }
            });
        }
        /**
         * 解析枚举
         * @param className 枚举名
         * @param code 待解析的数据
         * @returns {string}
         */
        ,getEnumValue : function (className,code) {
            var url = '/back/enum/getEnumValue';
            var name = "";
            $.ajax({
                url:url
                ,type:'post'
                ,data:{
                    enumClassName:className,
                    code:code
                }
                ,async:false
                ,dataType:'json'
                ,success:function(res){
                    if(res.code === return_200){
                        name = res.data;
                    }else {
                        console.log("枚举转义失败");
                        name = code;
                    }
                }
                ,error:function(){
                    console.log("枚举转义异常");
                    name = code;
                }
            });
            return name;
        }

    };
    exports("enums",obj);
});